
import {Tree} from 'element-ui'
import debounce from 'lodash/debounce.js'

export default {
  components: {Tree},
  props: {
    value: [Array, String, Number],
    dataUrl: String,
    queryParams: Object,
    multiple: Boolean,
    emptyText: String
  },
  data() {
    return {
      selectedKey: '', // 单选值
      checkedKeys: [], // 多选值
      checkedAll: '',
      dataRows: [],
      loading: false,
      errorMsg: '',
      initCurrValue: debounce(this._initCurrValue),
      queryDataRows: debounce(this._queryDataRows),
    }
  },
  created() {
    this.init()
  },
  mounted() {
    this.init()
  },
  watch: {
    value() {
      this.initCurrValue()
    },
    dataUrl() {
      this.init()
    },
    queryParams() {
      this.init()
    }
  },
  methods: {
    init() {
      this.selectedKey =''
      this.checkedKeys = []
      this.dataRows = []
      //
      this.initCurrValue()
      this.queryDataRows()
    },
    _initCurrValue() {
      if (this.multiple) {
        this.checkedKeys = []
        if (this.value && this.value.length && this.dataRows && this.dataRows.length) {
          this.value.forEach(key => {
            this.dataRows.forEach(row => {
              if (key == row.id) {
                this.checkedKeys.push(row.id)
              }
            })
          })
        }
        // console.log('initCurrValue: checkedKeys=%s', JSON.stringify(this.checkedKeys))
        if (this.checkedKeys.length == this.dataRows.length) {
          this.checkedAll = true
        } else {
          this.checkedAll = false
        }
      } else {
        if (this.value && this.value.length === 1) {
          this.selectedKey = this.value[0]
        }
      }
    },
    _queryDataRows() {
      if (!this.dataUrl) {
        this.errorMsg = '未配置数据接口！'
        return
      }
      this.loading = true
      this.$http.post(this.dataUrl, this.queryParams).then((rs) => {
        this.loading = false
        this.checkedAll = false
        const result = rs.data
        if (result.success) {
          this.dataRows = result.data
        } else {
          this.dataRows = []
        }
        this.initCurrValue()
      })
    },
    doRadioChange({target}) {
      // console.log('doRadioChange: %s', JSON.stringify(target))
      //
      let checkedRow = null
      this.dataRows.forEach(row => {
        if (target.value === row.id) {
          checkedRow = row
        }
      })
      this.$emit('onRadioChange', checkedRow, target.checked)
      this.$emit('input', this.selectedKey)
    },
    doCheckChange({target}) {
      // console.log('doCheckChange: %s, %s', target.value, target.checked)
      //
      let checkedRow
      this.dataRows.forEach(row => {
        if (row.id === target.value) {
          checkedRow = row
        }
      })
      if (this.checkedKeys.length === this.dataRows.length) {
        this.checkedAll = true
      } else {
        this.checkedAll = false
      }
      this.$emit('onCheckChange', checkedRow, target.checked)
      this.$emit('input', this.checkedKeys)
    },
    doCheckAll({target}) {
      this.checkedKeys = []
      if (target.checked) {
        this.dataRows.forEach(row => {
          this.checkedKeys.push(row.id)
        })
      }
      this.$emit('onCheck', this.dataRows, target.checked)
      this.$emit('input', this.checkedKeys)
    }
  }
}